

<template>
  <div class="login-container">
    <div  class="title">系统管理员登录</div>
    <el-form class="button" ref="loginForm" :model="loginForm" :rules="rules" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" autocomplete="off" style="margin-right: 5vh"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off" style="margin-right: 5vh"></el-input>
      </el-form-item >
      <el-form-item label="验证码" prop="authCode">
        <el-input v-model="loginForm.authCode" style="width: 50%" placeholder=""></el-input>
          <el-button :disabled="!loginForm.isSend"  @click="sendSmsCode(loginForm)" style="position: absolute;margin-left: 34.5vh" >
            {{ loginForm.smsCodeBtnText }}
          </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin(loginForm)" style="margin-left: 15vh">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script >
import {sysLoginService, userGetUser, userLoginService} from "@/api/user.js";
import {ElMessage} from "element-plus";
import useUserInfoStore from "@/stores/userInfo.js";
import {useTokenStore} from "@/stores/token.js";
import {useRouter} from "vue-router";
import userInfo from "@/stores/userInfo.js";
import router from "@/router/index.js";
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        authCode: '',
        smsCodeBtnText: '发送',
        isSmsCodeSending: false,
        isSend: true,
        router: useRouter()
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        authCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '长度为4个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {

    sendSmsCode(loginForm) {
      if (loginForm.username === undefined) {
        ElMessage.warning('请输入用户名')
        return
      }
      try {
        this.$refs.loginForm.isSmsCodeSending = true
        // await sendSmsCodeApi({username: this.$refs.loginForm.username})
        ElMessage.success('验证码已发送')
        this.startSmsCodeCountdown(loginForm)
      } catch (error) {
        ElMessage.error('验证码发送失败')
      } finally {
        loginForm.isSmsCodeSending = false
      }
    },
    startSmsCodeCountdown(loginForm){
      let count = 60

      const timer = setInterval(() => {

        if (count <= 0) {
          clearInterval(timer)
          loginForm.smsCodeBtnText = '发送'
          loginForm.isSend = true
        } else {
          loginForm.isSend = false
          loginForm.smsCodeBtnText = `${count}`
          count--
        }
      }, 1000)
    },
    handleLogin(loginForm) {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          let res = await sysLoginService(
            loginForm.username, loginForm.password, loginForm.authCode
          )
          ElMessage.success(res.message);
          useUserInfoStore().setInfo(res)
          useTokenStore().setToken(res.token)
          await loginForm.router.push('/layout')
        } else {
          ElMessage.error('登录失败!');
          return false;
        }
      });
    }

  },

};
</script>

<style scoped>
.title{
  text-align: center;
  font-size: 80px;
  color: #ace0f9;
  font-family: 华文新魏,serif;
}
.login-container {
  width: 100vh;
  height: 64vh;
  margin: 100px auto;
  background-image: url("../../assets/background.png");
  transform: scale(1.5, 1.2);
  border-radius: 2vh;
}
.button{
  margin-top: 6vh;
  margin-left: 20vh;
  height: 22vh;
  width: 60vh;
  position: relative;
  background: linear-gradient(135deg, rgba(255, 255, 255, var()), rgba(255, 255, 255, var()));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding-top: 50px;;
  border-radius: 10px;
}
</style>
